import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http'
import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
import { ENTRY } from '../../../enter';
import { BillItemsEntity, LabelValueEntity, stateResEntity, PostTransferRequest, PostBankHelp, PostPay, PostPayBank } from './recipy-details.entity';
@Injectable({
  providedIn: 'root'
})
export class RecipyDetailsService {
  private headers: HttpHeaders;
  constructor(private http: HttpClient) {
    this.headers = new HttpHeaders();
    this.headers.set('Content-Type', 'application/json');
  }
  private baseUrl = '/api/debt/getdebt/';
  private add2NameUrl = '/api/add2name/';
  private name2AddUrl = '/api/name2add/';
  private transferUrl = '/api/debt/transfer';
  private bankHelpUrl = '/api/debt/bankhelp';
  private payUrl = '/api/debt/pay';
  private payBankUrl = '/api/debt/paybank';
  public getDebtWholeInfo(name: string): Observable<stateResEntity> {
    return this.http.get<stateResEntity>(this.baseUrl + name, {
      headers: this.headers
    });
  }
  public add2Name(add: string): Observable<stateResEntity> {
    return this.http.get<stateResEntity>(this.add2NameUrl + add, {
      headers: this.headers
    });
  }

  public name2Add(name: string): Observable<stateResEntity> {
    return this.http.get<stateResEntity>(this.name2AddUrl + name, {
      headers: this.headers
    });
  }

  public postTransfer(from_name_: string, ori_to_name_: string, new_to_name_: string, money_: number ): Observable<stateResEntity> {
    let postData: PostTransferRequest;
    
    postData = { from_name: from_name_, ori_name: ori_to_name_, to_name:new_to_name_ , money: money_, time: 6000 };
    console.log('postData: ', postData);
    return this.http.post<stateResEntity>(this.transferUrl, postData);
  }

  public postBankHelp(from_name_: string, to_name_: string, money_: number): Observable<stateResEntity> {
    let postData: PostBankHelp;
    postData = {from_name: from_name_, to_name: to_name_, money: money_, time: 6000};
    return this.http.post<stateResEntity>(this.bankHelpUrl, postData);
  }

  public postPayHelp(from_name_: string, to_name_: string, money_: number): Observable<stateResEntity> {
    let postData: PostPay;
    postData = {from_name: from_name_, to_name: to_name_, money: money_};
    return this.http.post<stateResEntity>(this.payUrl, postData);
  }

  public postPayBank(name_: string, money_: number): Observable<stateResEntity> {
    let postData: PostPayBank;
    postData = {name: name_, money: money_};
    return this.http.post<stateResEntity>(this.payBankUrl, postData);
  }

  public getOptionalObject(name: string): LabelValueEntity[] {
    const children: LabelValueEntity[] = [
      {
        label: '蓝思科技',
        value: 'lansikeji'
      },
      {
        label: '华为',
        value: 'huawei'
      },
      {
        label: '京东方',
        value: 'jingdongfang'
      },
    ];
    let fin: LabelValueEntity[] = [];
    for (let i = 0; i < children.length; i++) {
      if (children[i].value != name) {
        fin.push(children[i]);
      }
    }
    return fin;
  }
}
